// pages/myself/balance/openMember.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();

Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    memberCardList: [],
    memberCard: {},
    isopen: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    
    wx.setNavigationBarTitle({
      title: "开通会员",
    })
    network._post_json(this, api.getMemberCardList, {}, success);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },


  //点击获取会员卡金额
  clickcard: function (e) {
    this.setData({
      isopen: 1,
      radioCheckVal: e.currentTarget.dataset.showimg,
      price: e.currentTarget.dataset.price
    })

    this.setData({
      memberCard: this.data.memberCardList[e.currentTarget.dataset.showimg]
    })
  },

  //点击获取会员卡金额
  pay: function (e) {
    network._post_json(this, api.recharge, { memberCardId: this.data.memberCard.id, totalFee: this.data.price }, paySuccess);
  },

}))

// 当前商品可使用优惠券
var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      memberCardList: that.data.memberCardList.concat(res.data.data),
    })
  }
}

// 会员卡充值
var paySuccess = function (that, res) {
  if (res.data.data) {
    // 购买
    network._post_json(that, api.wxRecharge, {
      appId: app.globalData.appId,
      orderId: res.data.data.orderId,
      OrderDesc: res.data.data.OrderDesc,
      totalFee: res.data.data.totalFee
    }, wxPaySuccess);
  }
}

// 微信支付结果
var wxPaySuccess = function (that, res) {
  if (res.data.data) {
    wx.requestPayment({
      timeStamp: res.data.data.timeStamp,
      nonceStr: res.data.data.nonceStr,
      package: res.data.data.package,
      signType: res.data.data.signType,
      paySign: res.data.data.paySign,
      success: function (res) {
        // wx.redirectTo({
        //   url: "/pages/details/payOver?payMoney=" + that.data.speciesPrice.price / 100 * that.data.count
        // })
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      },
    });
  }
}